package server.exportimport;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import abstractservlet.AbstractServlet;
import autoservice.api.controller.IAutoservice;
import controllers.Autoservice;

@WebServlet("/Import")
public class Import extends AbstractServlet {
	private static final long serialVersionUID = 1L;
	private IAutoservice autoservice = Autoservice.getInstance();
	private static final String IMPORT = "import";
	private static final String URL = "url";
	private static final String MASTERS = "masters";
	private static final String ORDERS = "orders";
	private static final String IMPORT_PAGE = "/exportimportpages/ImportPage.jsp";
	private static final String MASTER_URL = "masterUrl";
	private static final String GARAGE_URL = "garageUrl";
	private static final String DELIMITER = " ";

	public Import() {
		super();

	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String object = request.getParameter(IMPORT);
		String path = request.getParameter(URL);
		String answer = null;
		if (object.equals(MASTERS)) {
			answer = autoservice.importMasters(path).get(0);
		} else if (object.equals(ORDERS)) {

			answer = autoservice.importOrders(path,
					request.getParameter(MASTER_URL),
					request.getParameter(GARAGE_URL)).get(0);
		} else {
			answer = autoservice.importGarages(path).get(0);
		}

		super.navigate(request, response, IMPORT +DELIMITER+ object, answer, IMPORT_PAGE);

	}

}
